/***********************************************************
 *
 * Gaoth
 * Copyright (c) 2011- Marcus Lundgren
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from
 * the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it freely,
 * subject to the following restrictions:
 *
 *  1. The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software
 *     in a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *
 *  2. Altered source versions must be plainly marked as such,
 *     and must not be misrepresented as being the original software.
 *
 *  3. This notice may not be removed or altered from any source distribution.
 *
 ***********************************************************/

#include "game_state.h"

#include <string>
#include <iostream>

irr::scene::IAnimatedMesh* mesh;
irr::scene::IAnimatedMeshSceneNode* node1;
irr::scene::IAnimatedMeshSceneNode* node2;

GameState::GameState( irr::IrrlichtDevice *window, irr::video::IVideoDriver *driver, irr::scene::ISceneManager *scene_man,
        irr::gui::IGUIEnvironment *gui_env, Model *model, NetworkManager *net_man )
: IState( window, driver, scene_man, gui_env, model ), _net_man( net_man )
{
    _net_man->connect(sf::IpAddress("127.0.0.1"));

    mesh = _scene_mgr->getMesh("../resources/sydney.md2");
}

GameState::~GameState()
{
    // We are not the owner
    _net_man = 0;
}

void GameState::init()
{
    _is_done = false;

    _gui_env->clear();
    _scene_mgr->clear();

    node1 = _scene_mgr->addAnimatedMeshSceneNode( mesh );

    node1->setMaterialFlag(irr::video::EMF_LIGHTING, false);
    node1->setMD2Animation(irr::scene::EMAT_STAND);
    node1->setMaterialTexture( 0, _driver->getTexture("../resources/sydney.bmp") );

    node2 = _scene_mgr->addAnimatedMeshSceneNode( mesh );

    node2->setMaterialFlag(irr::video::EMF_LIGHTING, false);
    node2->setMD2Animation(irr::scene::EMAT_STAND);
    node2->setMaterialTexture( 0, _driver->getTexture("../resources/sydney.bmp") );

    _scene_mgr->addCameraSceneNode(0, irr::core::vector3df(0,30,-80), irr::core::vector3df(0,5,0));
}

void GameState::update( const float /*time*/ )
{
    /*gth::shared::network::LoginPayload p;
    _net_man->send(p);*/
}

typedef std::basic_string<wchar_t> wstring;

void GameState::draw() const
{
    std::vector<gth::shared::data::BaseEntity> &entities = _model->entities;
    if( entities.size() == 2 )
    {
        node1->setPosition(irr::core::vector3df(entities[0].pos.x, entities[0].pos.y, 0.f));
        node2->setPosition(irr::core::vector3df(entities[1].pos.x, entities[1].pos.y, 0.f));
        //node1->setPosition(irr::core::vector3df(10.f, 0.f, 0.f));
        //node2->setPosition(irr::core::vector3df(-10.f, 0.f, 0.f));
    }
    /*std::string s = _model->get_last_message();
    std::cout << "Message is: " << s << "\n";

    wstring ws;

    for( std::string::iterator it = s.begin() ; it != s.end() ; ++it )
        ws.push_back(*it);*/

    _driver->beginScene(true, true, irr::video::SColor(255,100,101,140));

    _scene_mgr->drawAll();
    _gui_env->drawAll();

    _driver->endScene();
}
